package com.Haitch.MaximWidget;

import com.Haitch.MaximWidget.R;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.SharedPreferences;
import android.widget.RemoteViews;

public class MaximWidgetProvider extends AppWidgetProvider {
	public static final String URI_SCHEME = "gnome_widget";
	@Override    
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;

        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];
            SharedPreferences config = context.getSharedPreferences(MaximWidgetConfigure.PREFS_NAME, 0);
            String gnomeText = config.getString(String.format(MaximWidgetConfigure.PREFS_UPDATE_GNOME_FIELD_PATTERN, appWidgetId), context.getString(R.string.txt_defaultmaxim));
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.typicalwidget);
            views.setTextViewText(R.id.textView1, gnomeText);
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }
    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        for (int appWidgetId : appWidgetIds) {
            SharedPreferences config = context.getSharedPreferences(MaximWidgetConfigure.PREFS_NAME, 0);
            SharedPreferences.Editor configEditor = config.edit();

            configEditor.remove(String.format(MaximWidgetConfigure.PREFS_UPDATE_GNOME_FIELD_PATTERN, appWidgetId));
            configEditor.commit();
        }

        super.onDeleted(context, appWidgetIds);
    }
}